package com.czh.myprinter;

import android.os.AsyncTask;

import com.google.gson.Gson;
import okhttp3.*;

import java.io.IOException;
import java.util.Map;

/**
 * @program: printer
 * @description:
 * @author: czh
 * @create: 2024-07-26 16:31
 **/

public class HttpRequest{

    private final OkHttpClient client = new OkHttpClient();
    private final Gson gson = new Gson(); // 使用Gson来序列化Map为JSON字符串


    public String post(String url, Map<String, Object> params) throws IOException {
        String json = gson.toJson(params); // 将Map转换为JSON字符串
        RequestBody body = RequestBody.create(json, MediaType.get("application/json"));
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();

        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }


}
